<template>
	<!-- 我的 -->
	<view class="" v-if="time<time2"></view>
	<view v-else>
		<view class="user flex-cb">
			<view class="user_img flex align-center">
				<image src="/static/logo.png" mode="" v-if="!!user.avater"></image>
				<image :src="user.avatar" mode="" v-else style="border-radius: 50%;"></image>
				<view class="user_name">
					<view class="name">{{user.nickname?user.nickname:'微信用户'}}</view>
					<view class="phone">{{user.phone}}
						<view class="agent flex-cb">
							<view v-if="user.vip_expire_time">vip会员</view>
							<view v-if="user.agent.apply_status == 1">
								{{user.agent.lv==1?user.agent.province+'代':user.agent.lv==2?user.agent.city+'代':user.agent.city+user.agent.district+'代'}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<navigator url="/pageC/mine/user" class="user_setup flex align-center">
				<image src="https://guizhouchenxun.com/mine/setup.png" mode=""></image>
				<text>设置</text>
			</navigator>
		</view>
		<!-- 物流 -->
		<view class="button3 flex-cb">
			<view class="button_item flex  justify-center" v-for="(item,index) in datelist" :key="index"
				@click="navigate(item.url)">
				<image :src="item.Image" mode="widthFix"></image>
				<view class="text">{{item.text}}</view>
				<view class="has-news" v-if="item.orderNum!=0">
					{{item.orderNum}}
				</view>
			</view>
		</view>
		<!-- 功能列表 -->
		<view class="list">
			<view class="list_item flex-cb" @click="toWriteOff()" v-if="isAdmin">
				<view class="flex align-center">
					<image class="img" src="https://guizhouchenxun.com/mine/hx.png" mode="widthFix"></image>
					<text>核销订单</text>
				</view>
				<view class="flex-cb">
					<image class="icon" src="https://guizhouchenxun.com/mine/icon.png" mode=""></image>
				</view>
			</view>
			<view class="list_item flex-cb" v-for="(item,index) in functionlist" :key="index"
				@click="handlelist(item.url)">
				<view class="flex align-center">
					<image class="img" :src="item.Image" mode="widthFix"></image>
					<text>{{item.text}}</text>
				</view>
				<view class="flex-cb">
					<view style="font-size: 28rpx;margin-right: 10rpx;"
						v-if="is_endtime.settled_fee_is_open==1&&item.text=='商家入驻'&&t<10">
						入驻时间到期，
						<text style="color: #FF2E2E;">去续费</text>
					</view>
					<view class="ticket-num" v-if="item.url == 'voucher' && ticketNum != 0">
						{{ticketNum}}
					</view>
					<image class="icon" src="https://guizhouchenxun.com/mine/icon.png" mode=""></image>
				</view>

			</view>
		</view>
		<view class="versions">
			版本号：2.1.27
		</view>
		<!-- 协议 -->
		<view class="agreement modal_view" v-if="isAgent">
			<view class="agreement_text bg-white">
				<scroll-view scroll-y="true" class="screen-swiper">
					<view v-html="Agent"></view>
				</scroll-view>
				<view class="colse flex align-center">
					<image src="/static/gou.png" mode="" v-if="!isAgent" @click="handleAgent"></image>
					<image src="/static/gou2.png" mode="" @click="handleAgent"></image>
					<text>阅读并同意《协议条款》</text>
				</view>
			</view>
		</view>
		<!-- 客服 -->
		<view class="mobilePhone modal_view" v-if="isshow">
			<view class="phone_num" @click="cphone">{{phone}}</view>
			<view class="cancel" @click="handlelist('none')">取消</view>
		</view>
	</view>
</template>

<script>
	// import {
	// 	mutations as uniIdMutations
	// } from '@/uni_modules/uni-id-pages/common/store.js'
	// uniIdMutations.logout()
	import {
		write_off,
		getIsAdminApi,
		getBVoucherListApi
	} from "@/common/api.js"
	export default {
		data() {
			return {
				// 抵用券数量
				ticketNum: 0,
				t: 0, //入驻到期时间
				isshow: 0,
				isAgent: 0,
				Agent: '',
				phone: '',
				is_endtime: 0,
				user: {},
				shop: '', //商家信息
				datelist: [{
						text: '物流配送',
						Image: 'https://guizhouchenxun.com/mine/logistics.png',
						url: 'WL_Delivery',
						orderNum: 0
					},
					{
						text: '商家配送',
						Image: 'https://guizhouchenxun.com/mine/sj.png',
						url: 'SJ_order',
						orderNum: 0
					},
					{
						text: '自提订单',
						Image: 'https://guizhouchenxun.com/mine/order.png',
						url: 'ZT_order',
						orderNum: 0
					},
					{
						text: '点餐订单',
						Image: 'https://guizhouchenxun.com/mine/order2.png',
						url: 'DC_order',
						orderNum: 0
					}
				],
				functionlist: [{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '商家入驻',
						Image: 'https://guizhouchenxun.com/mine/1.png',
						url: 'pageA/admin'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '抵用券',
						Image: 'https://guizhouchenxun.com/mine/9.png',
						url: 'voucher'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '地址管理',
						Image: 'https://guizhouchenxun.com/mine/2.png',
						url: 'address'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '我要代理',
						Image: 'https://guizhouchenxun.com/mine/3.png',
						url: 'agent'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '分销中心',
						Image: 'https://guizhouchenxun.com/mine/4.png',
						url: 'distribution'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '我的积分',
						Image: 'https://guizhouchenxun.com/mine/5.png',
						url: 'integral'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '我的会员',
						Image: 'https://guizhouchenxun.com/mine/6.png',
						url: 'vip'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '官方客服',
						Image: 'https://guizhouchenxun.com/mine/7.png',
						url: 'none'
					},
					{
						icon: 'https://guizhouchenxun.com/mine/icon.png',
						text: '退出登录',
						Image: 'https://guizhouchenxun.com/mine/8.png',
						url: 'nologin'
					}
				],
				time: '',
				time2: '',
				isAdmin: false,
			};
		},
		methods: {
			// 获取现金券数
			getBVoucherNum() {
				getBVoucherListApi({
					is_use: 0
				}).then(res => {
					this.ticketNum = res.data.length
				})
			},
			// 是否为管理员
			getIsAdmin() {
				getIsAdminApi().then(res => {
					this.isAdmin = res.data
				})
			},
			navigate(url) { //订单跳转
				if (uni.getStorageSync('access_token') == '') {
					this.$utils.isgologin()
				} else {
					uni.navigateTo({
						url: '/pageC/WL_Delivery/' + url,
					})
				}
			},
			// 核销订单
			toWriteOff() {
				uni.scanCode({
					scanType: ["qrCode"],
					success: (res) => {
						if (res.result) {
							const q = decodeURIComponent(res.result) // 获取到二维码原始链接内容
							let t = JSON.parse(JSON.stringify(q))
							var Request = new Object();
							Request = this.GetRequest(t);
							write_off({}, Request.id, false).then(e => {
								if (e.code == 200001) {
									uni.showToast({
										title: '核销成功',
										icon: 'none'
									})
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none'
									})
								}
							})
						} else {
							console.log('请重新扫描');
							return false;
						}
					},
					fail: (res) => {
						console.log('未识别到二维码');
					}
				})
			},
			GetRequest(url) {
				let theRequest = new Object();
				if (url.indexOf("?") != -1) {
					let str = url.substr(1);
					let strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
					}
				}
				return theRequest;
			},
			handlelist(url) {
				console.log(url);
				console.log(this.user, '当前用户信息');
				if (uni.getStorageSync('access_token') == '') {
					this.$utils.isgologin()
					return
				}
				if (url == 'none') {
					this.isshow = !this.isshow
					return
				}
				if (url == 'nologin') { //退出登陆
					uni.showModal({
						confirmColor: '#1CD699',
						content: '是否确认退出登录吗？',
						title: '退出登录',
						success: (res) => {
							if (res.confirm) {
								uni.setStorageSync("openid", '');
								uni.setStorageSync('userinfo', '')
								uni.setStorageSync("expires_in", '');
								uni.setStorageSync("token_type", '');
								uni.setStorageSync("access_token", '');
								// uniIdMutations.logout()
								uni.switchTab({
									url: '/pages/tabbar/index',
								})
							}
						}
					})
					return
				}
				if (url == 'agent') {
					// if(uni.getStorageSync('isAgent') !== 0){
					// 	this.isAgent = 1
					// 	return
					// }
					if (this.user.agent == null) {
						uni.navigateTo({
							url: '/pageC/mine/Function list/agent'
						})
						return
					}
					if (this.user.is_agent == 1) {
						uni.showToast({
							title: '你已经是' + this.user.agent.city + this.user.agent.district + '代理',
							icon: 'none'
						})
						return
					}
					if (this.user.agent.apply_status == 0) {
						uni.showToast({
							title: '你已经提交过申请啦~',
							icon: 'none'
						})
						return
					} else if (this.user.agent.apply_status == -1) {
						let that = this
						uni.showModal({
							title: '您的代理申请被拒绝',
							content: '拒绝原因：' + that.user.agent.apply_reject_reason + ',是否重新申请',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pageC/mine/Function list/agent'
									})
								}
							}
						})
						return
					}
				}
				if (url == 'SettleIn') {
					if (this.shop) {
						console.log("=======settled_expiry_time========" + this.shop.settled_expiry_time);
						if (this.is_endtime.settled_fee_is_open == 1 && this.user.is_mch == 1) { //收费模式
							uni.navigateTo({
								url: '/pageC/mine/Function list/' + url + 2
							})
							return
						}
						if (this.is_endtime.settled_fee_is_open == 0 && this.user.is_mch == 1 && this.t <= 0) {
							uni.showToast({
								title: '您的入驻已到期，请联系平台管理员续费',
								icon: 'none'
							})
							return
						}
					}
				}
				if (url == 'pageA/admin') {
					if (this.user.is_mch == 0) {
						uni.navigateTo({
							url: '/pageC/mine/Function list/SettleIn'
						})
						return
					}
					uni.navigateTo({
						url: '/pageA/index'
					})
				}
				uni.navigateTo({
					url: '/pageC/mine/Function list/' + url + '?agent=' + JSON.stringify(this.user.agent)
				})
			},
			handleAgent() {
				this.isAgent = 0
				uni.setStorageSync('isAgent', this.isAgent)
				// uni.navigateTo({
				// 	url:'../mine/Function list/agent' 
				// })
				// uni.setStorageSync(agent,0)
			},
			// 更新im用户信息
			updateIMUserInfo() {
				let upQuery = {
					nick: this.user.nickname,
					avatar: this.user.avatar,
					gender: this.user.gender == 0 ? uni.$TUIKitTypes.GENDER_FEMALE : uni.$TUIKitTypes.GENDER_MALE,
				}
				console.log(upQuery, '更新资料：：：');
				uni.$TUIKit.updateMyProfile(upQuery)
					.then(function(imResponse) {
						console.log(imResponse.data, '更新资料成功'); // 更新资料成功
					}).catch(function(imError) {
						console.warn(
							'更新资料失败',
							imError); // 更新资料失败的相关信息
					});
			},
			getuser() {
				this.$api.user({}, true).then(res => {
					if (res.code == 200001) {
						this.user = res.data
						if (this.user.avatar != "") {
							this.user.avatar = this.$base + res.data.avatar
						}
						this.updateIMUserInfo()
						this.$api.merchants_info({}, 1).then(res => { //商家信息
							if (res.code == 200001) {
								this.shop = res.data
								if (this.shop.settled_expiry_time) {
									const setTime = new Date(this.shop.settled_expiry_time);
									const nowTime = new Date();
									const restSec = setTime.getTime() - nowTime.getTime();
									const day = parseInt(restSec / (60 * 60 * 24 * 1000));
									this.t = day
								}
							}
						})
					}
				})
			},
			cphone() {
				this.isshow = 0
				uni.makePhoneCall({
					phoneNumber: this.phone,

				})
			}
		},
		onLoad() {
			this.time = Date.parse(new Date()) / 1000
			this.time2 = this.$time
			if (this.time < this.time2) {
				uni.navigateTo({
					url: '/pages/index'
				})
			}
			this.$api.agency().then(res => {
				this.Agent = res.data
			})
			// this.$api.agentsApplyInfos().then(res=>{//获取代理状态
			// 	this.user.status = res.data
			// })
			this.$api.service_phone().then(res => {
				this.phone = res.data
			})
		},
		onPullDownRefresh() {
			this.getuser()
			uni.stopPullDownRefresh()
		},
		onShow() {
			// if(!!uni.getStorageSync("hasNews")){
			// 	uni.showTabBarRedDot({ //显示红点
			// 		index: 2,
			// 	})
			// }
			this.getuser()
			this.getIsAdmin()
			this.$api.unfinished1().then(res => {
				this.datelist[0].orderNum = res.data
			})
			this.$api.unfinished2().then(res => {
				this.datelist[1].orderNum = res.data
			})
			this.$api.unfinished3().then(res => {
				this.datelist[2].orderNum = res.data
			})
			this.$api.unfinished4().then(res => {
				this.datelist[3].orderNum = res.data
			})
			uni.setTabBarStyle({
				backgroundColor: '#fff'
			})
			if (uni.getStorageSync('access_token') == '') {
				this.$utils.isgologin()
			}
			this.$api.merchants_settled_rule().then(res => {
				this.is_endtime = res.data
			})

			this.getBVoucherNum()

		}
	}
</script>

<style lang="scss">
	.user {
		background-color: #fff;
		padding: 63rpx 0 63rpx 34rpx;

		.user_img {
			image {
				width: 128rpx;
				height: 128rpx;
			}

			.user_name {
				margin-left: 23rpx;

				.name {
					margin-bottom: 16rpx;
					font-size: 38rpx;
					color: #333333;
					font-weight: bold;
				}

				.phone {
					position: relative;
					color: #999999;
					font-size: 28rpx;

					.agent {
						position: absolute;
						bottom: -50rpx;
						left: 0;

						font-size: 22rpx;
						color: #fff;

						view {
							margin-right: 10rpx;
							white-space: nowrap;
							min-height: 40rpx;
							background-color: #1CD699;
							border-radius: 20rpx;
							padding: 5rpx 10rpx;
						}
					}
				}

			}

		}

		.user_setup {
			background-color: RGBA(231, 251, 245, 1);
			width: 167rpx;
			height: 70rpx;
			border-radius: 70rpx 0px 0px 70rpx;

			image {
				width: 60rpx;
				height: 60rpx;
				margin-left: 9rpx;
			}

			text {
				font-size: 30rpx;
				color: #1CD699;
				font-weight: 500;
				margin-left: 12rpx;
			}
		}

	}

	.button3 {
		margin: 10rpx 0;
		height: 206rpx;
		background-color: #fff;
		padding: 65rpx 48rpx 18rpx 48rpx;

		.button_item {
			position: relative;
			width: 119rpx;
			flex-wrap: wrap;

			image {
				width: 78rpx;
				height: 66rpx;
				margin-bottom: 22rpx;
			}

			image:nth-last-of-type(3) {
				width: 96rpx;
				height: 59rpx;
			}

			view {
				white-space: nowrap;
			}

		}

	}

	.has-news {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		height: 30rpx;
		width: 30rpx;
		background: red;
		flex-shrink: 0;
		border-radius: 50%;
		font-size: 24rpx;
		color: #fff;
		text-align: center;
		line-height: 30rpx;
	}

	.list {
		background-color: #fff;
		padding: 0 39rpx 0 36rpx;

		// margin-bottom:114rpx ;
		.list_item {
			height: 97rpx;
			width: 690rpx;
			border-bottom: #EBEBEB solid 2rpx;

			.img {
				width: 40rpx;
				height: 40rpx;
				margin-right: 22rpx;
				font-weight: 400;
			}

			text {
				color: #333333;
				font-size: 30rpx;
			}

			.icon {
				width: 16rpx;
				height: 30rpx;
			}
		}

		.list_item:nth-last-of-type(1) {
			border-bottom: 0 solid #000;
		}

	}

	.mobilePhone {
		view {
			width: 710rpx;
			height: 116rpx;
			background: #FFFFFF;
			border-radius: 14rpx;
			position: fixed;
			left: 21rpx;
			bottom: 158rpx;
			text-align: center;
			line-height: 116rpx;
		}

		.phone_num {
			font-size: 30rpx;
			color: #1CD699;
			font-weight: bold;
		}

		.cancel {
			font-size: 32rpx;
			font-weight: 500;
			bottom: 26rpx;
		}
	}

	.agreement {
		.agreement_text {
			width: 100%;
			height: calc(100vh - 70rpx);
			position: absolute;
			bottom: 0;

			.screen-swiper {
				height: calc(100vh - 110rpx);

				view {
					padding: 29rpx 26rpx 0 26rpx;
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 70rpx;
				}
			}

			.colse {
				position: fixed;
				bottom: 20rpx;
				width: 100%;
				height: 110rpx;
				background: #FFFFFF;
				box-shadow: 0px -3px 10px 1px rgba(0, 0, 0, 0.07);

				image {
					width: 40rpx;
					height: 40rpx;
					margin-left: 48rpx;
					margin-right: 20rpx;
				}
			}
		}
	}

	.versions {
		padding: 30rpx;
		text-align: center;
	}

	.ticket-num {
		width: 30rpx;
		height: 30rpx;
		background-color: rgb(28,214,153);
		color: #fff;
		border-radius: 10rpx;
		font-size: 22rpx;
		align-items: center;
		display: flex;
		justify-content: center;
		margin-right: 15rpx;
	}
</style>
